<?php
class cls_control_discount extends cls_control_base
{
	function show()
	{
		global $smarty;
		$smarty->display('discount/discount.htm');
		
	}
	
	function discount_list_show()
	{
		$permission_serve = parent::get_serve('permission');
		$permission_serve->check_permission('DISCOUNT_INFO');
		$page = $_POST['page'];
		$rp = $_POST['rp'];
		$sortname = $_POST['sortname'];
		$sortorder = $_POST['sortorder'];
		
		if (!$sortname) $sortname = 'name';
		if (!$sortorder) $sortorder = 'desc';
		if($_POST['query']!=''){
			$where = "WHERE ".$_POST['qtype']." LIKE '%".$_POST['query']."%' ";
			p($where);
		} else {
			$where ='';
		}
		if($_POST['letter_pressed']!=''){
			$where = "WHERE ".$_POST['qtype']." LIKE '".$_POST['letter_pressed']."%' ";
		}
		if($_POST['letter_pressed']=='#'){
			$where = "WHERE ".$_POST['qtype']." REGEXP '[[:digit:]]' ";
		}
		$sort = "ORDER BY $sortname $sortorder";
		
		$discount_serve = parent::get_serve();
		$discount_list  = $discount_serve->get_discount_list($page,$rp,$where,$sort);
		$total  =$discount_serve->getCount($where);
		
		header("Last-Modified: " . gmdate( "D, d M Y H:i:s" ) . "GMT" );
		
		header("Content-type: text/x-json");
		$json = "";
		$json .= "{\n";
		$json .= "page: $page,\n";
		$json .= "total: $total,\n";
		$json .= "rows: [";
		$rc = false;
		$i = 0;
		while($i<count($discount_list) )
		{
			if ($rc) $json .= ",";
			$json .= "\n{";
			$json .= "ID:'".$discount_list[$i]['ID']."',";
			$json .= "cell:['".$discount_list[$i]['ID']."','".$discount_list[$i]['DiscountType']."'";
			$json .= ",'".addslashes($discount_list[$i]['Name'])."'";
			$json .= ",'".addslashes($discount_list[$i]['Description'])."'";
			$json .= ",'".addslashes($discount_list[$i]['Amount'])."'";
			$json .= ",'".addslashes($discount_list[$i]['StartTime'])."'";
			$json .= ",'".addslashes($discount_list[$i]['EndTime'])."'";
			$json .= ",'".addslashes($discount_list[$i]['Status'])."']";
				
			$json .= "}";
			$rc = true;
			$i++;
		}
		$json .= "]\n";
		$json .= "}";
		
		echo $json;
		
		
	}
	
	function discount_product_show()
	{
		
		$permission_serve = parent::get_serve('permission');
		$permission_serve->check_permission('DISCOUNT_INFO');
		$page = $_POST['page'];
		$rp = $_POST['rp'];
		$sortname = $_POST['sortname'];
		$sortorder = $_POST['sortorder'];
		
		if (!$sortname) $sortname = 'name';
		if (!$sortorder) $sortorder = 'desc';
		if($_POST['query']!=''){
			
			$where = "WHERE ".$_POST['qtype']." LIKE '%".$_POST['query']."%' ";
		} else {
			$where ='';
		}
		if($_POST['letter_pressed']!=''){
			$where = "WHERE `".$_POST['qtype']."` LIKE '".$_POST['letter_pressed']."%' ";
		}
		if($_POST['letter_pressed']=='#'){
			$where = "WHERE `".$_POST['qtype']."` REGEXP '[[:digit:]]' ";
		}
		$sort = "ORDER BY $sortname $sortorder";
		
		$discount_serve = parent::get_serve();
		$discount_list  = $discount_serve->get_discount_product($page,$rp,$where,$sort);
		$total  =$discount_serve->getCount($where,'discountproduct',1);
		
		
		header("Last-Modified: " . gmdate( "D, d M Y H:i:s" ) . "GMT" );
		
		header("Content-type: text/x-json");
		$json = "";
		$json .= "{\n";
		$json .= "page: $page,\n";
		$json .= "total: $total,\n";
		$json .= "rows: [";
		$rc = false;
		$i = 0;
		while($i<count($discount_list) )
		{
			if ($rc) $json .= ",";
			$json .= "\n{";
			$json .= "ID:'".$discount_list[$i]['ID']."',";
			$json .= "cell:['".$discount_list[$i]['ID']."','".$discount_list[$i]['pname']."'";
			$json .= ",'".addslashes($discount_list[$i]['dname'])."'";
			
			$json .= ",'".addslashes($discount_list[$i]['amount'])."']";
		
			$json .= "}";
			$rc = true;
			$i++;
		}
		$json .= "]\n";
		$json .= "}";
		
		echo $json;
		
		
	} 
	
	function get_discount_product()
	{
		
		$permission_serve = parent::get_serve('permission');
		$permission_serve->check_permission('DISCOUNT_INFO');
		global $smarty;
		$this->smarty->display('discount/discount_product.htm');
		
		
	}
	function get_discount_list()
	{
		$permission_serve = parent::get_serve('permission');
		$permission_serve->check_permission('DISCOUNT_INFO');
		global $smarty;
		$this->smarty->display('discount/discount_list.htm');
	}
	
	
	function discount_delete()
	{
		$permission_serve = parent::get_serve('permission');
		$permission_serve->check_permission('DISCOUNT_MANAGE','DISCOUNT_INFO');
		$items = rtrim($_POST['items'],",");
		$discount_serve = parent::get_serve();
		$discount_serve->delete_discount($items);

		echo json_encode(null);
	}
	//add a discount for a category
	function add_discount()
	{
		//$type = $_POST['type'];
        //$promotion = $_POST['promotion'];
		$permission_serve = parent::get_serve('permission');
		$permission_serve->check_permission('DISCOUNT_MANAGE','DISCOUNT_INFO');
		$items = rtrim($_POST['items'],",");
		$discount_serve = parent::get_serve();
		$discount_serve->add_discount($items);
		
		echo json_encode(null);
	}
	
	
	//add a discount for certain products
	function product_add_discount()
	{
		$permission_serve = parent::get_serve('permission');
		$permission_serve->check_permission('DISCOUNT_MANAGE','DISCOUNT_INFO');
		$items = rtrim($_POST['items'],",");
		$discount_serve = parent::get_serve();
		$discount_serve->product_add_discount($items);
		
		echo json_encode(null);
	}
	function modify_discount()
	{
		$permission_serve = parent::get_serve('permission');
		$permission_serve->check_permission('DISCOUNT_MANAGE','DISCOUNT_INFO');
		$items = $_POST['items'];
		$discount_serve = parent::get_serve();
		$discount_serve->modify_discount($items);
		echo json_encode(null);
	}
}